<template>
  <div>
    <van-search
      v-model="value"
      show-action
      placeholder="请输入搜索关键词"
      @search="onSearch"
      @focus="qufangFn"
    >
      <template #action>
        <div @click="onSearch">搜索</div>
      </template>
    </van-search>
    <div class="box" v-if="statu">
        <p>搜索历史....</p>
        <div v-for="(item,index) in historys" :key="index" class="hisbox">
            <span class="hisonbox" @click="research(item)">{{item}}</span>
        </div>
        <span class="delehis" @click="removeHistory">删除历史记录</span>
    </div>
    <div v-if="kong"><span id="kongy">空</span></div>
    <div v-if="!statu">
        <div v-for="item in obj" :key="item.id" @click="toDetail(item.id)">
            <!-- 左右结构 -->
        <div class="single" v-if="item.type==1&&item.cover.length<3">
          <div class="left">
            <p class="content" v-html="item.title"></p>
            <p class="info">
              <span>{{item.user.nickname}}</span>
              <span>{{item.comment_length}}</span>
            </p>
          </div>
          <img :src="item.cover[0].url" alt />
        </div>


        <!-- 视频结构 -->
        <div class="single2" v-if="item.type==2">
          <p class="content" v-html="item.title"></p>
          <div class="playarea">
            <img :src="item.cover[0].url" alt />
            <div class="playicon">
              <van-icon name="play" />
            </div>
          </div>
          <p class="info">
            <span>{{item.user.nickname}}</span>
            <span>{{item.comment_length}}</span>
          </p>
        </div>
        <!-- 上下结构 -->
        <div class="single3" v-if="item.type==1&&item.cover.length>=3">
          <p class="content" v-html="item.title"></p>
          <div class="imgs">
            <img alt v-for="item in item.cover" :key="item.id" :src="item.url"/>
          </div>
          <p class="info">
            <span>{{item.user.nickname}}</span>
            <span>{{item.comment_length}}</span>
          </p>
        </div>
        </div>
    </div>
  </div>
</template>

<script>
import {searchPage} from '@/api/user'
export default {
    data(){
        return{
            value:'',
            obj:{},
            historys:[],
            statu:true,
            kong:false
        }
    }, 
    created(){
        this.historys=JSON.parse(localStorage.getItem('history'))||[]
    },
    methods:{
        onSearch(){
            this.obj={}
            this.statu=false //执行搜索就改变statu,隐藏搜索历史
            if(this.value==='')return
            if(this.historys.indexOf(this.value)==-1){
                this.historys.push(this.value)
                localStorage.setItem('history',JSON.stringify(this.historys))
            }
            searchPage({keyword:this.value}).then(res=>{
                if(res.data.total===0){
                    this.kong=true //如果没有找到数据就显示空
                }else{
                    this.obj=res.data.data
                    this.obj.forEach((item)=>{
                        item.title=item.title.replaceAll(this.value,`<span style='color:red'>${this.value}</span>`)
                    })
                }
            })
        },
        research(value){
            this.value=value
            this.onSearch()
        },
        removeHistory(){
            localStorage.removeItem('history')
            this.historys=[]
        },
        toDetail(id){
            this.$router.push({
                path:'/articleDetail',
                query:{
                    id
                }
            })
        },
        qufangFn(){
            this.statu=true
            this.kong=false
        }
    }
}
</script>

<style lang="less" scoped>
#kongy{
    margin: 20px 0 0 50%;
    color: rgba(102, 102, 102, 0.57);
}
.box{
    margin: 15px;
    p{
        color:rgba(102, 102, 102, 0.57);
        font-size: 14px;
    }
    .hisbox{
        margin: 10px 0;
        display: inline-block;
        .hisonbox{
            margin-right: 10px;
            padding: 2px 8px;
            height: 25px;
            line-height: 18px;
            background-color: rgba(194, 173, 176, 0.254);
            border-radius: 9px;
            color:#666;
        }
    }
    .delehis{
        margin: 0 auto;
        width: 90px;
        margin-top: 15px;
        display: block;
        text-align: center;
        font-size: 14px;
        color: #666;
    }
}
/deep/.van-nav-bar .van-icon {
    color: #333;
    font-size: 20px;
}
.info {
  font-size: 12px;
  padding-left: 5px;
  color: #999;
  > span:nth-of-type(1) {
    padding-right: 15px;
  }
}
.content {
  font-size: 14px;
  padding: 0px 5px;
  line-height: 24px;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
}


.single {
  padding: 15px 0px;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid #ccc;
  flex-wrap: wrap;
  .left {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    overflow: hidden;
  }


  img {
    width: (120/360) * 100vw;
    height: (70/360) * 100vw;
    object-fit: cover;
    padding-right: 5px;
  }
}
.single2 {
  border-bottom: 1px solid #ccc;
  padding: 8px 0;
  .playarea {
    width: 100%;
    position: relative;
    margin-bottom: 10px;
    img {
      width: 100%;
      display: block;
    }
    .playicon {
      width: 60px;
      height: 60px;
      border-radius: 50%;
      background-color: rgba(0, 0, 0, 0.4);
      box-shadow: 0px 0px 15px #fff;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      display: flex;
      justify-content: center;
      .van-icon {
        font-size: 40px;
        color: #fff;
        line-height: 60px;
      }
    }
  }
}
.single3 {
  width: 100vw;
  .imgs {
    width: 100%;
    display: flex;
    padding: 10px 0 0 0;
    img {
      width: (120/360) * 100vw;
      height: (70/360) * 100vw;
      // 让图片自动的调整大小，根据父容器的大小自动调整
      object-fit: cover;
      padding-right: 5px;
    }
  }
}
</style>